<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<?
require_once '../classes/ConnectionDB.php';
require_once '../classes/UtenteManager.php';
require_once '../classes/TestDataManager.php';
require_once '../bean/Argomento.php';
$connection = ConnectionDB::getInstance()->connectToDB();
$codiceUtente = $_COOKIE['codiceUtente'];



if (isset($_GET["fromUpdate"]))
{
    $nomeTest = $_GET["nome"];
    $oreTest = $_GET["ore"];
    $minutiTest = $_GET["minuti"];
    $codiceTest = $_GET["codiceTest"];
    $test = TestDataManager::getInstance()->getTestByCodice($connection, $codiceTest);
    $domandeGiaInserite = TestDataManager::getInstance()->getDomandeByCodiceTest($connection, $codiceTest);
}
else
{
    $nomeTest = $_POST["nome"];
    $oreTest = $_POST["ore"];
    $minutiTest = $_POST["minuti"];
    $codiceDisciplina = $_POST["codiceDisciplina"];
}

$result = UtenteManager::getInstance()->getUtente($connection, $codiceUtente);

$argomenti = TestDataManager::getInstance()->getArgomenti($connection, $codiceUtente);
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Nuovo Test - step 2 - </title>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/prototype.js" type="text/javascript"></script>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/scriptaculous/effects.js" type="text/javascript"></script>
        <script src="../glassboxLatest/glassbox.2.0.0/javascripts/glassbox/glassbox.js" type="text/javascript"></script>
        <style type="text/css">
            @import url("../css/style.css");
            table{
                width: 100%;
            }

            #tableResults td{
                text-align: center;
            }


            #searchForm {
                padding-top: 3%;
                margin-left: -5px;
            }

            #searchFormCont
            {
                padding-left: 2%;
            }

            #navigator
            {
                font-size: 14px;
            }

        </style>
        <script type="text/javascript">
            path_to_root_dir = "../glassboxLatest/glassbox.2.0.0/";
            var listaDomande;
            var numItem=10;
            var currentIndex=0;
            var listaDomandePresenti ="";

            window.onload = function()
            {

<? if (isset($_GET["fromUpdate"]))
{ ?>
            listaDomandePresenti = new Array();
            var indiceDomande=0;
    <?
    $dom = explode("*", $domandeGiaInserite);

    for ($k = 0; $k < count($dom); $k++)
    {
        ?>
          listaDomandePresenti[indiceDomande] = "<?= $dom[$k] ?>";
          indiceDomande++;
        <? }
} ?>

            var searchForm = new GlassBox();
            searchForm.init( 'searchForm', '850px', '290px', 'hidden','',true, false);
            searchForm.ipos();
            searchForm.appear();

            var results = new GlassBox();
            results.init( 'results', '1040px', '390px', 'auto','',true,false);
            results.ipos();
            results.appear();
        }

        function xXMLHttpRequest()
        {

            var client = null;
            if (window.XMLHttpRequest)
            {
                client=new XMLHttpRequest();
            }
            // code for IE
            else if (window.ActiveXObject)
            {
                client=new ActiveXObject("Microsoft.XMLHTTP");
            }

            return client;
        }



        function findQuestions()
        {
            var client = xXMLHttpRequest();
            client.onreadystatechange = function()
            {
                if(this.readyState == 4 && this.status == 200)
                {
                    listaDomande = new Array();
                    var resp = eval(this.responseText);
                    if(resp != null)
                    {
                        for(i=0;i<resp.length; i++)
                        {
                            listaDomande[i] = resp[i];
                        }
                    }
                    document.getElementById('btnConferma').disabled=false;
                    manageNavigatore();




                } else if (this.readyState == 4 && this.status != 200) {
                    // fetched the wrong page or network error...
                    alert("error this.status : " + this.status );

                }
            }
            var codiceArgomento = document.getElementById('codiceArgomento').value;
            var tipologia = document.getElementById('tipologia').value;
            var domandaLike =document.getElementById('domandaLike').value;

            var param = "codiceArgomento="+ codiceArgomento +
                "&tipologia="+tipologia+
                "&domandaLike="+domandaLike;
            client.open("POST", "../report/reportQuestionsListener.php", true);
            client.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

            client.send(param);

        }

        function manageNavigatore()
        {

            var tmp;
            var str="<table border='1' id='tableResults' cellpadding='5'>"
                +"<tr>"
                +"<th>Codice domanda</th>"
                +"<th>Domanda</th>"
                +"<th>Argomento</th>"
                +"<th>Tipologia</th>"
                +"<th>Dettaglio</th>"
                +"<th>Inserisci in test</th>"
                +"</tr>";
            var indCurrenct=0;
            while(indCurrenct<listaDomande.length)
            {

                tmp=listaDomande[indCurrenct];
                var nomeTipologia;
                if(tmp.tipologia=='1')
                    nomeTipologia="Vero/Falso";
                else
                    nomeTipologia="Risposta multipla";
                str+="<tr><td>"+tmp.codiceDomanda+"</td>"+
                    "<td>"+tmp.testoDomanda+"</td>"+
                    "<td>"+tmp.nomeArgomento+"</td>"+
                    "<td>"+nomeTipologia+"</td>"+
                    "<td><a href='../report/detailQuestion.php?codiceDomanda="+tmp.codiceDomanda+"'>Vedi dettaglio</a></td>";

                if(isInQuestions(tmp.codiceDomanda))
                    str += "<td><input type='checkbox' id='inTest_"+indCurrenct+"' name='inTest' value='"+tmp.codiceDomanda+"' checked disabled'/></td></tr>";
                else
                    str += "<td><input type='checkbox' id='inTest_"+indCurrenct+"' name='inTest' value='"+tmp.codiceDomanda+"'  /></td></tr>";


                indCurrenct++;
            }
            str +="</table>";

            document.getElementById('resultsCont').innerHTML=str;
        }


        function isInQuestions(domanda)
        {
            var tmp2;
            for(i=0;i<listaDomandePresenti.length;i++)
            {
                tmp2=listaDomandePresenti[i].split("*");
                //    alert("domanda  " + domanda + " tmp2[0] " + tmp2[0]);
                if(tmp2[0]==domanda)
                    return true;
            }
            return false;
        }

        function showDetail(codice)
        {
            location.href="detailQuestion.php?codiceDomanda=" + codice;
        }

        function continua()
        {
            var s;
            var checks="";
            for (s=0; s<listaDomande.length;s++)
                if(document.getElementById('inTest_'+s).checked)
                    checks += (listaDomande[s]).codiceDomanda+"*";

            if(checks=="")
                location.href='../portal/manageErrors.php?lista=23*';
            else
            {
<? if (isset($_GET['fromUpdate']))
{ ?>

                document.getElementById('checksUp').value=checks;
                document.getElementById('codiceTestUp').value=<?= $codiceTest ?>;
                document.getElementById('addQuestionToTest').submit();
    <?
}
else
{
    ?>
              document.getElementById('checks').value=checks;
              document.getElementById('testForm2').submit();
    <? } ?>

          }
      }
        </script>
    </head>
    <body>
        <form id="addQuestionToTest" method="POST" action="newTestListener.php">
            <input type="hidden" name="operation" value="addQuestion" />
            <input type="hidden" name="checks" id="checksUp" value="" />
            <input type="hidden" name="codiceTest" id="codiceTestUp" value="" />
            <input type="hidden" name="filtered" id="filtered" value="" />
            <input type="hidden" name="nomeTest" value="<?= $nomeTest ?>" />
            <input type="hidden" name="oreTest" value="<?= $oreTest ?>" />
            <input type="hidden" name="minutiTest" value="<?= $minutiTest ?>" />
        </form>

        <form id="testForm2" method="POST" action="newTest_3.php">
            <input type="hidden" name="checks" id="checks" value="" />
            <input type="hidden" name="nomeTest" value="<?= $nomeTest ?>" />
            <input type="hidden" name="oreTest" value="<?= $oreTest ?>" />
            <input type="hidden" name="minutiTest" value="<?= $minutiTest ?>" />
            <input type="hidden" name="codiceDisciplina" value="<?= $codiceDisciplina ?>" />


        </form>
        <?php include '../portal/header.php'; ?>
        <center>

            <div id="searchForm">
                <div id="searchFormCont">
                    <table cellpadding="3" style="display: block; padding-left: 70%">

                        <tr>
                            <td colspan="2">&nbsp;</td>
                            <td style="color: gray;">Step 1 - Dettaglio test</td>
                        </tr>
                        <tr>
                            <td colspan="2">&nbsp;</td>
                            <td style="color: #3e7ad4;"><strong>Step 2 - Scelta domande</strong></td>
                        </tr>
                        <tr>
                            <td colspan="2">&nbsp;</td>
                            <td style="color: gray">Step 3 - Conferma test</td>
                        </tr>
                    </table>
                    <table style="padding-left: 14%;" cellpadding="3">

                        <tr>
                            <td>Argomento:</td>
                            <td><select name="codiceArgomento" id="codiceArgomento">
                                    <option value="">Tutti</option>
                                    <?
                                    for ($j = 0; $j < count($argomenti); $j++)
                                    {
                                        ?>
                                    <option value="<?= $argomenti[$j]->getCodice() ?>"><?= $argomenti[$j]->getNome() ?></option>
                                        <? } ?>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>Tipologia</td>
                            <td>
                                <select name="tipologia" id="tipologia">
                                    <option value="">Tutte</option>
                                    <option value="1">Vero / Falso</option>
                                    <option value="2">Risposta multipla</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>Domanda contiene:</td>
                            <td><input type="text" name="domandaLike" id="domandaLike" /></td>
                        </tr>
                    </table>
                    <br/>
                    <input type="button" value="Cerca" onclick="javascript:findQuestions();" />

                    <input type="button" id="btnConferma" value="Continua" onclick="continua();"  disabled />


                </div>
            </div>
            <div id="results"><div id="resultsCont"></div></div>
        </center>
    </body>
</html>
